# -*- coding: utf-8 -*-

'''
    【简介】 tkinter基本使用:选择文件与文件另存

'''

import tkinter as tk
from tkinter import filedialog
import pandas as pd


class GUI:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('演示窗口')
        self.root.geometry("500x200+1100+150")
        self.interface()

    def interface(self):
        """"界面编写位置"""
        self.Button0 = tk.Button(self.root, text="选择单个文件", command=self.single_file)
        self.Button0.grid(row=0, column=0)

        self.Button1 = tk.Button(self.root, text="选择多个文件", command=self.multiple_files)
        self.Button1.grid(row=0, column=1)

        self.Button2 = tk.Button(self.root, text="保存文件", command=self.save_file)
        self.Button2.grid(row=0, column=2)

        self.w1 = tk.Text(self.root, width=68, height=10)
        self.w1.grid(row=1, column=0, columnspan=3, padx=10)

    def single_file(self):
        '''获取单个文件'''
        file_path = filedialog.askopenfilename()
        self.w1.insert("insert", file_path)

    def multiple_files(self):
        '''获取多个文件'''
        file_path = filedialog.askopenfilenames()
        self.w1.insert("insert", file_path)

    def save_file(self):
        '''保存文件'''
        # 去掉title='保存为'，标题默认名称“另存为”
        file_name = filedialog.asksaveasfilename(title='保存为', filetypes=[("csv", ".csv")])
        # 文件添加数据并保存
        filename = file_name + '.csv'
        csvfile = open(filename, 'w')
        name = ['姓名', '年龄', '电话']
        list1 = [
            ('王明', '25', '12345678'),
            ('张三', '18', '87654321')
        ]
        save = pd.DataFrame(columns=name, data=list1)
        save.to_csv(filename)


if __name__ == '__main__':
    gui = GUI()
    gui.root.mainloop()
